package com.zhoux.regex;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/10 10:48
 */
public class Regex_Test {
    public static boolean ifPhone(String phone){
        // 该正则表达式只判断17X,19X,13X和15X段的手机号
//        String regex = "((13\\d)|(19\\d)|(17\\d)|(15\\d))\\d{8}";
        String regex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";
        // 将一个字符串编译成 Pattern 对象
        Pattern pattern = Pattern.compile(regex);
        // 使用 Pattern 对象创建 Matcher 对象
        Matcher matcher = pattern.matcher(phone);
        boolean bool = matcher.matches(); // 返回 true or false
        if (bool){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入E-mail地址:");
        String phone = in.nextLine();
        boolean bool;
        bool = ifPhone(phone);
        if (bool){
            System.out.print("输入E-mail地址正确！");
        }else{
            System.out.print("输入E-mail地址有误！");
        }
    }
}

